package com.example.day34.controller;


import com.example.day34.model.PageInfos;
import com.example.day34.model.User;
import com.example.day34.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("user")
@AllArgsConstructor
public class UserController {
    private final IUserService userService;

    @RequestMapping("index")
    public String index(Model model){
        model.addAttribute("list",userService.list());
        return "user/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public List<User> list(){
        return userService.list();
    }

    @RequestMapping("add")
    @ResponseBody
    public int add(User user){
        return userService.add(user);
    }

    @RequestMapping("update")
    @ResponseBody
    public int update(User user){
        return userService.upadte(user);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int delete(Integer id){
        return userService.delete(id);
    }
    @RequestMapping("batchdelete")
    @ResponseBody
    public int add(String ids){
        List<Integer> idList = Arrays.asList(ids.split(",")).stream().map(item -> Integer.parseInt(item)).collect(Collectors.toList());
        return userService.batchDelete(idList);
    }


    @RequestMapping("/pageInfos")
    @ResponseBody
    public PageInfos<User> pageInfos(Integer pageNum, Integer pageSize,Integer id){
        PageInfos<User> page = userService.Page(pageNum, pageSize);
        return page;
    }


}


